package com.hp.gxw.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hp.gxw.entity.SysMenu;
import com.hp.gxw.entity.SysRole;
import com.hp.gxw.entity.SysRolePermission;
import com.hp.gxw.entity.SysUser;
import com.hp.gxw.service.ISysMenuService;
import com.hp.gxw.service.ISysRoleService;
import com.hp.gxw.service.ISysUserService;
import com.hp.gxw.service.ISysRolePermissionService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author blink
 */
public class FreeRealm extends AuthorizingRealm {

	@Autowired
	private ISysUserService iSysUserService;

	@Autowired
	private ISysRolePermissionService ISysRolePermissionService;

	@Autowired
	private ISysMenuService iSysMenuService;
	
	@Autowired
	private ISysRoleService iSysRoleService;

	/** 授权访问控制 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

		SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

		// 用户信息
		SysUser user = (SysUser) principalCollection.getPrimaryPrincipal();
		// 获取用户角色信息 多角色
		String[] roleIds = user.getRoleId().split(",");

		QueryWrapper<SysRolePermission> ew = new QueryWrapper<SysRolePermission>();
		ew.in("role_id", Arrays.asList(roleIds));
		// 角色权限关联表
		List<SysRolePermission> role_permissions = ISysRolePermissionService.list(ew);
		// 权限列表Id
		Set<String> permissionIdSet = new HashSet<String>();
		for (SysRolePermission sysRolePermission : role_permissions) {
			permissionIdSet.add(sysRolePermission.getPermissionId());
		}
		Set<String> permissionSet = new HashSet<String>();
		if (permissionIdSet.size() != 0) {
			List<SysMenu> permissionList = iSysMenuService.list(new QueryWrapper<SysMenu>().in("id", permissionIdSet));
			for (SysMenu permission : permissionList) {
				permissionSet.add(permission.getId());
			}
		}
		// 赋予权限
		if (permissionSet.size() != 0) {
			simpleAuthorizationInfo.addStringPermissions(permissionSet);
		}

		return simpleAuthorizationInfo;
	}

	/** 验证用户身份 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
			throws AuthenticationException {
		// 用户名
		String username = (String) authenticationToken.getPrincipal();

		SysUser user = iSysUserService.getOne(new QueryWrapper<SysUser>().eq("user_code", username));
		if (user == null) {
			return null;
		}
//		user.setDepartmentName(iSysDeptService.selectInfo(new QueryWrapper<SysDept>().eq("id", user.getDepartmentId())).getDeptName());
		user.setRoleName(iSysRoleService.selectInfoOne(new QueryWrapper<SysRole>().eq("id", user.getRoleId())).getRoleName());

		return new SimpleAuthenticationInfo(user, user.getUserPassword(), null, getName());
	}

}